package dongze.com.ckdj.service.uploader;

import android.util.Log;

import java.io.File;
import java.util.List;

import dongze.com.ckdj.dagger.component.ServiceComponent;
import dongze.com.ckdj.data.DataLayer;
import dongze.com.ckdj.data.entity.request.UploadFileRequestBody;
import dongze.com.ckdj.data.webinterface.ImageUploadInterface;
import dongze.com.ckdj.map.MapUtil;
import dongze.com.ckdj.persistence.dao.DaoSession;
import dongze.com.ckdj.persistence.dao.Image;
import dongze.com.ckdj.persistence.dao.MassLossUploadRecord;
import dongze.com.ckdj.persistence.dao.MassLossUploadRecordDao;
import dongze.com.ckdj.persistence.dao.ServerUploadTask;
import dongze.com.ckdj.service.ImageUploadTask;
import dongze.com.ckdj.util.MyLog;
import io.reactivex.ObservableEmitter;
import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;


/**
 * Created by fanqwz on 2017/8/21.
 */

public class MassLossUploader extends Uploader<String, String> {
    private static final String TAG = MassLossUploader.class.getSimpleName();
    private ServerUploadTask task;
    MapUtil mapUtil;
    String massLossRecord;
    String imageIdex;
    String userName;

    public MassLossUploader(Scheduler subscribeThread, Scheduler observerThread, ServerUploadTask task, MapUtil mapUtil, DataLayer dataLayer, DaoSession session, ImageUploader.ImageProgressListener listener) {
        super(AndroidSchedulers.mainThread(), observerThread);
        MassLossUploadRecord record = session.getMassLossUploadRecordDao().queryBuilder().where(MassLossUploadRecordDao.Properties.Id.eq(task.getRecordId())).uniqueOrThrow();
        this.task = task;
        this.mapUtil = mapUtil;
        this.massLossRecord = record.getMassLossRecord();
        this.imageIdex = record.getImageIndex();
        this.userName = record.getUser();

        dependsOn(new ImageUploader(subscribeThread,
                observerThread,
                dataLayer,
                session.getImageDao(),
                task.getImage(),
                listener));
    }

    @Override
    Worker<String, String> getWorker() {
        return worker;
    }

    Worker<String, String> worker = new Worker<String, String>() {
        String imageRet;

        @Override
        public void accept(String depends) {
            imageRet = depends;
        }

        @Override
        public void doWork(final ObservableEmitter<String> e) {
            if (imageRet == null) {
                e.onError(new Exception("upload mass loss but image return null"));
                return;
            }

            mapUtil.updateMassLossImageRecord(massLossRecord, imageIdex, imageRet, userName, new DisposableObserver<String>() {
                @Override
                public void onNext(String value) {
                    if (value != null) {
                        MyLog.d(TAG, "onNext: " + massLossRecord + ":" + value);
                        e.onNext(value);
                    } else {
                        e.onError(new Exception(MassLossUploader.class.getSimpleName() + ":do work failed"));
                    }
                }

                @Override
                public void onError(Throwable ex) {
                    MyLog.e(TAG, "onError: " + massLossRecord + "[" + imageIdex + ":" + imageRet + "]");
                    e.onError(ex);
                }

                @Override
                public void onComplete() {
                    MyLog.d(TAG, "onComplete: " + massLossRecord + "[" + imageIdex + ":" + imageRet + "]");
                    e.onComplete();
                }
            });
        }
    };
}
